package com.wulee.notebook.ui;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.wulee.notebook.R;
import com.wulee.notebook.adapter.NoteListAdapter;
import com.wulee.notebook.bean.Constant;
import com.wulee.notebook.bean.Note;
import com.wulee.notebook.db.NoteDao;
import com.wulee.notebook.db.db.DBUtls;
import com.wulee.notebook.utils.AppUtils;
import com.wulee.notebook.view.SpacesItemDecoration;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

import static com.wulee.notebook.bao.App.aCache;


/**
 * 描述：主界面
 */

public class MainActivity extends BaseActivity implements View.OnClickListener{

    final Context context = this;
    private String key;
    private static final String TAG = "MainActivity";
    private SwipeRefreshLayout swipeLayout;
    private RecyclerView rv_list_main;
    private TextView tvNodata;
//    private FloatingActionMenu fabMenu;
//    private FloatingActionButton fabCheckUpdate,fabLogout,fabAboutMe;
    private NoteListAdapter mNoteListAdapter;
    private List<Note> noteList;
    private NoteDao noteDao;
    private DrawerLayout mDrawerDl;//侧滑菜单布局控件
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        initView();

        long lastShowNoticeTime = 0L;
        try {
            String timeStr = aCache.getAsString(Constant.KEY_LAST_CHECK_UPDATE_TIME);
            if(!TextUtils.isEmpty(timeStr)){
                lastShowNoticeTime = Long.parseLong(timeStr);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        long interal = System.currentTimeMillis() - lastShowNoticeTime;
    }

    protected void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);

        noteDao = new NoteDao(this);

        swipeLayout = findViewById(R.id.swipeLayout);
        swipeLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorAccent));
        rv_list_main =  findViewById(R.id.rv_list_main);
        tvNodata =  findViewById(R.id.tv_nodata);
//        fabMenu =  findViewById(R.id.fab_menu);
//        fabCheckUpdate =  findViewById(R.id.fab_check_update);
//        fabLogout =  findViewById(R.id.fab_logout);
//        fabAboutMe=  findViewById(R.id.fab_about_me);
        rv_list_main.addItemDecoration(new SpacesItemDecoration(0));//设置item间距
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//竖向列表
        rv_list_main.setLayoutManager(layoutManager);


        mNoteListAdapter = new NoteListAdapter(R.layout.list_item_note,noteList);
        rv_list_main.setAdapter(mNoteListAdapter);
        mDrawerDl=(DrawerLayout)findViewById(R.id.left_menu_dl);


        mNoteListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final Note note = (Note) adapter.getData().get(position);
                if (note.getIsEncrypt() > 0) {
                    // get prompts.xml view
                    LayoutInflater li = LayoutInflater.from(context);
                    View promptsView = li.inflate(R.layout.pass_alert, null);

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            context);

                    // set prompts.xml to alertdialog builder
                    alertDialogBuilder.setView(promptsView);

                    final EditText userInput = (EditText) promptsView
                            .findViewById(R.id.key);

                    // set dialog message
                    alertDialogBuilder
                            .setCancelable(false)
                            .setNegativeButton("确认",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {
                                            /** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
                                            key = (userInput.getText()).toString();

                                            /** CHECK FOR USER'S INPUT **/
                                            // check password here
                                            Intent intent = new Intent(MainActivity.this, NoteActivity.class);
                                            Bundle bundle = new Bundle();
                                            bundle.putSerializable("key", key);
                                            bundle.putSerializable("note", note);
                                            intent.putExtra("data", bundle);
                                            startActivity(intent);
                                        }
                                    })
                            .setPositiveButton("取消",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {
                                            dialog.dismiss();
                                        }

                                    }

                            );

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();


                } else {
                    Intent intent = new Intent(MainActivity.this, NoteActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("note", note);
                    intent.putExtra("data", bundle);
                    startActivity(intent);
                }
            }
        });

        mNoteListAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                final Note noteObj = (Note) adapter.getData().get(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示");
                builder.setMessage("确定删除笔记？");
                builder.setCancelable(false);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String objectId = null;
                        if(noteList != null && noteList.size()>0){
                            Note noteInfo = noteList.get(position);
                            objectId = noteInfo.getId();
                        }
                        int ret = DBUtls.deleteNote(noteObj);
                        if (ret > 0){
                            showToast("删除成功");
                            refreshNoteList(false);
                        }
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
                return false;
            }
        });

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshNoteList(true);
            }
        });

//        fabAboutMe.setOnClickListener(this);
//        fabCheckUpdate.setOnClickListener(this);
//        fabLogout.setOnClickListener(this);
        findViewById(R.id.fab_note_list_add).setOnClickListener(this);
    }

    private void setDrawer(){
        //创建返回键，并实现打开关/闭监听
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerDl, mToolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerToggle.syncState();
        mDrawerDl.setDrawerListener(mDrawerToggle);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    //刷新笔记列表
    private void refreshNoteList(final boolean isRefresh){
        if(!isRefresh){
            showProgressBar("正在加载...");
        }
        noteList = DBUtls.queryNotesAll();
        mNoteListAdapter.setNewData(noteList);
        hideProgressBar();
        if(isRefresh){
            swipeLayout.setRefreshing(false);
        }
        if(noteList != null && noteList.size()>0){
            tvNodata.setVisibility(View.GONE);
        }else{
            tvNodata.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshNoteList(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_new_note:
                Intent intent = new Intent(MainActivity.this, NewActivity.class);
                intent.putExtra("flag", 0);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showLogoutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("确定要退出吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                aCache.put("has_login","no");

//                AppUtils.AppExit(MainActivity.this);
//                UserInfo.logOut();
//                startActivity(new Intent(MainActivity.this,LoginActivity.class));
            }
        });
        builder.setNegativeButton("取消", null);
        builder.create().show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.fab_check_update:
//                checkUpdate();
//                break;
//            case R.id.fab_logout:
//                showLogoutDialog();
//                break;
//            case R.id.fab_about_me:
//                startActivity(new Intent(this,AboutMeActivity.class));
//                break;
            case R.id.fab_note_list_add:
                startActivity(new Intent(this,NewActivity.class));
                break;
        }
    }

    private long mLastClickReturnTime = 0l; // 记录上一次点击返回按钮的时间
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if(System.currentTimeMillis() - mLastClickReturnTime > 1000L) {
                    mLastClickReturnTime = System.currentTimeMillis();
                    showToast("再按一次退出程序");
                    return true;
                }
            }
        }
        return super.dispatchKeyEvent(event);
    }

}
